home *** CD-ROM | disk | FTP | other *** search
- /*LINTLIBRARY*/
-
- /* @(#)procs.c 1.25 91/11/13
- *
- * Procedures associated with the reve game panel items.
- *
- * Copyright (C) 1990, 1991 - Rich Burridge & Yves Gallot.
- * All rights reserved.
- *
- * Permission is granted to copy this source, for redistribution
- * in source form only, provided the news headers in "substantially
- * unaltered format" are retained, the introductory messages are not
- * removed, and no monies are exchanged.
- *
- * Permission is also granted to copy this source, without the
- * news headers, for the purposes of making an executable copy by
- * means of compilation, provided that such copy will not be used
- * for the purposes of competition in any othello tournaments, without
- * prior permission from the authors.
- *
- * No responsibility is taken for any errors on inaccuracies inherent
- * either to the comments or the code of this program, but if reported
- * (see README file), then an attempt will be made to fix them.
- */
-
- #include "color.h"
- #include "reve.h"
- #include "extern.h"
-
-
- void
- display_help()
- {
- char str[4] ;
- int val ;
-
- val = items[itemno].value ;
- if (direction == INCREMENT && help_offsets[++val] != -1)
- items[itemno].value++ ;
- else if (direction == DECREMENT && help_offsets[--val] != -1)
- items[itemno].value-- ;
- else return ;
- SPRINTF(str, "%d", items[itemno].value) ;
- set_cycle(W_HELP, HELP_PAGE, str) ;
- paint_help_text() ;
- }
-
-
- void
- do_edit()
- {
- message(PANEL_MES, "Not currently implemented.") ;
- }
-
-
- void
- do_help()
- {
- help_showing = !help_showing ;
- set_frame(W_HELP, help_showing) ;
- }
-
-
- void
- do_stop()
- {
- message(PANEL_MES, "Not currently implemented.") ;
- }
-
-
- void
- do_props()
- {
- props_showing = !props_showing ;
- set_frame(W_PROPS, props_showing) ;
- }
-
-
- void
- do_selection(state)
- int state ;
- {
- int cx, cy ;
-
- cx = (piece_x + pieceXrad - bborder) / cell_width ;
- cy = (piece_y + pieceYrad - bborder) / cell_height ;
-
- set_cursor(CANVASCUR) ;
- if (state == LEFT_DOWN)
- {
- draw_outline(cy * BOARD_SIZE + cx, IS_ON) ;
- if (ANIMATION) draw_piece(next_player, piece_x, piece_y, RINV) ;
- cmode = (enum cantype) ((int) cmode - 1) ;
- }
- else
- {
- if (ANIMATION) draw_piece(next_player, piece_x, piece_y, RINV) ;
- draw_outline(last_outline, IS_OFF) ;
- move = cy * BOARD_SIZE + cx ;
- update_clock(next_player, TRUE) ;
- make_move() ;
- }
- }
-
-
- void
- do_suggest(player, move, note, state)
- int player, move, note ;
- enum bltype state ;
- {
- int count ;
-
- set_cursor(CANVASCUR) ;
-
- if (move == -1) return ;
- if (best_cmove != -1 && DO_BESTMOVE)
- {
- draw_square(best_cmove, IS_OFF, 2) ;
- if (DO_NUMBER) show_number(best_cmove, cmove_depth, IS_OFF) ;
- }
-
- draw_symbol(S_SUGGEST, move, state) ;
- if (show_moves && state == IS_OFF) draw_symbol(S_MOVE, move, IS_ON) ;
- if (show_moves || state == IS_ON)
- {
- count = legal(move, player, &board) ;
- show_number(move, count, IS_ON) ;
- }
- if (SHOW_NOTES && state == IS_ON) set_eval(player, move, note) ;
- }
-
-
- void
- init_help_file(helpfile) /* Load reve help page offsets. */
- char *helpfile ;
- {
- char buf[MAXLINE] ;
- int i, page ;
- long offset ;
-
- for (i = 0; i < MAXPAGES; i++) help_offsets[i] = -1 ;
- if ((hfp = find_file(helpfile)) == NULL)
- {
- FPRINTF(stderr, "Cannot open online help file\n") ;
- return ;
- }
- page = 1 ;
- for (;;)
- {
- offset = ftell(hfp) ;
- if (fgets(buf, MAXLINE, hfp) == NULL) return ;
- if (EQUAL(buf, "REVE(6)")) help_offsets[page++] = offset ;
- }
- }
-
-
- void
- make_move()
- {
- if (legal(move, next_player, &board) == 0)
- {
- invalid = TRUE ;
- message(PANEL_MES, "Invalid move") ;
- cmode = (enum cantype) ((int) cmode - 1) ;
- if (SHOW_LEGAL) show_all(IS_ON) ;
- return ;
- }
- do_move(next_player) ;
- invalid = FALSE ;
-
- switch (dtype)
- {
- case XBLACK :
- case XWHITE : opponent_move(OPPONENT(next_player)) ;
- break ;
- case XBOTH : if (check(OPPONENT(next_player)) == TRUE) break ;
- (void) check(next_player) ;
- return ;
- }
- cmode = (enum cantype) (OPPONENT(next_player) + 1) ;
- next_player = OPPONENT(next_player) ;
- }
-
-
- void
- new_game()
- {
- initboard() ;
- cmode = BLACK_START ;
- last_move = -1 ;
- if (items[(int) BLACK_PLAYS].value == COMPUTER) first_move = 1 ;
- init_clocks() ;
- paint_all() ;
- message(EVAL_MES, "") ;
- message(PANEL_MES, "Use left mouse button to move") ;
- draw_button(W_PANEL, NEW_GAME_BUT, C_ITEMS, BUT_NORMAL) ;
- }
-
-
- void
- opt_toggle(val)
- int *val ;
- {
- if (*val) *val = FALSE ;
- else *val = TRUE ;
- }
-
-
- void
- redo()
- {
- enum panel_type p ;
-
- /* The amount we redo is dependant upon two things:
- *
- * 1/ Whether the opponent is the computer, in which case we redo
- * two "moves". Otherwise we just redo one "move".
- *
- * 2/ A "move" is considered to be all consecutive moves by the opponent.
- */
-
- redo_move(next_player) ;
- p = (next_player == BLACK) ? BLACK_PLAYS : WHITE_PLAYS ;
- if (items[(int) p].value == COMPUTER) redo_move(next_player) ;
-
- set_score() ;
- set_turn(next_player) ;
- }
-
-
- void
- redo_move(player)
- int player ;
- {
- int i, m, n, x, y ;
-
- m = n = 64 - board.moves_left ;
- while (moves[n].move != -1 && moves[n].player == player) n++ ;
- n-- ;
-
- if (n < m || n > 63) message(PANEL_MES, "No moves to redo.") ;
- else
- {
- if (DO_LAST) show_last(last_move, IS_OFF) ;
- if (DO_NUMBER) show_number(last_move, 60 - board.moves_left, IS_OFF) ;
- if (show_moves) show_all(IS_OFF) ;
- do_suggest(player, suggestion, note, IS_OFF) ;
- FOR_BOARD(i)
- {
- if (moves[n].square[i] != board.square[i])
- {
- get_xy(i, &x, &y) ;
- if (moves[n].square[i] == FREE)
- color_area(W_BOARD, x, y,
- 2 * pieceXrad, 2 * pieceYrad, W_COLOR(C_SQUARE)) ;
- else draw_piece(moves[n].square[i], x, y, RSRC) ;
- }
- board.square[i] = moves[n].square[i] ;
- }
- board.moves_left = moves[n].moves_left ;
- board.player = moves[n].player ;
- board.move = moves[n].move ;
- board.note = moves[n].note ;
- board.timeleft = moves[n].timeleft ;
- last_move = board.move ;
-
- FOR_BOARD(i) old_board.square[i] = moves[n-1].square[i] ;
- old_board.moves_left = moves[n-1].moves_left ;
- old_board.player = moves[n-1].player ;
- old_board.move = moves[n-1].move ;
- old_board.note = moves[n-1].note ;
- old_board.timeleft = moves[n-1].timeleft ;
-
- timeleft = board.timeleft ;
- reset_time(timeleft) ;
-
- if (OPPONENT(player) == BLACK) cmode = BLACK_START ;
- else cmode = WHITE_START ;
- message(PANEL_MES, "") ;
-
- if (DO_LAST) show_last(last_move, IS_ON) ;
- if (DO_NUMBER) show_number(last_move, 60 - board.moves_left, IS_ON) ;
- if (SHOW_NOTES) set_eval(board.player, board.move, board.note) ;
- else message(EVAL_MES, "") ;
- next_player = OPPONENT(board.player) ;
- }
- }
-
-
- void
- set_computer(val)
- int val ;
- {
- int curi ;
-
- if (val == CP_WHITE)
- {
- dtype = XBLACK ;
- play_computer = TRUE ;
- items[(int) BLACK_PLAYS].value = HUMAN ;
- STRCPY(items[(int) BLACK_PLAYS].text, player_values[HUMAN]) ;
- items[(int) WHITE_PLAYS].value = COMPUTER ;
- STRCPY(items[(int) WHITE_PLAYS].text, player_values[COMPUTER]) ;
- }
- else if (val == CP_BLACK)
- {
- dtype = XWHITE ;
- play_computer = TRUE ;
- items[(int) BLACK_PLAYS].value = COMPUTER ;
- STRCPY(items[(int) BLACK_PLAYS].text, player_values[COMPUTER]) ;
- items[(int) WHITE_PLAYS].value = HUMAN ;
- STRCPY(items[(int) WHITE_PLAYS].text, player_values[HUMAN]) ;
- }
- else if (val == CP_NEITHER)
- {
- dtype = XBOTH ;
- play_computer = FALSE ;
- items[(int) BLACK_PLAYS].value = HUMAN ;
- STRCPY(items[(int) BLACK_PLAYS].text, player_values[HUMAN]) ;
- items[(int) WHITE_PLAYS].value = HUMAN ;
- STRCPY(items[(int) WHITE_PLAYS].text, player_values[HUMAN]) ;
- }
-
- draw_choice(W_PROPS, COMP_CHOICE, W_COLOR(C_PANEL)) ;
- make_message(W_PANEL, BLACK_PLAYS) ;
- make_message(W_PANEL, WHITE_PLAYS) ;
-
- if (next_player == BLACK) curi = items[(int) BLACK_PLAYS].value ;
- else curi = items[(int) WHITE_PLAYS].value ;
- if (curi == COMPUTER)
- if ((next_player == BLACK && cmode == BLACK_START) ||
- (next_player == WHITE && cmode == WHITE_START))
- opponent_move(next_player) ;
- }
-
-
- void
- set_option()
- {
- char str[4] ;
- int maxw, n, val ;
-
- switch (itemno)
- {
- case COMP_CHOICE : maxw = items[itemno].width / items[itemno].nopts ;
- val = (curx - items[itemno].x) / maxw ;
- if (val != CP_BOTH)
- {
- items[itemno].value = val ;
- set_computer(val) ;
- }
- else message(PANEL_MES,
- "Computer plays both is not implemented yet.") ;
- break ;
-
- case DIFF_CHOICE : maxw = items[itemno].width / items[itemno].nopts ;
- val = (curx - items[itemno].x) / maxw ;
- level = val + 1 ;
- items[itemno].value = val ;
- draw_choice(W_PROPS, DIFF_CHOICE, W_COLOR(C_PANEL)) ;
-
- items[(int) MAX_DEPTH].value = 0 ;
- set_cycle(W_PROPS, MAX_DEPTH, "off") ;
- break ;
-
- case MAX_DEPTH : val = items[itemno].value ;
- if (direction == INCREMENT && val < MAX_PROFMAX)
- items[itemno].value++ ;
- else if (direction == DECREMENT && val > 0)
- items[itemno].value-- ;
- if (!items[(int) itemno].value) STRCPY(str, "off") ;
- else SPRINTF(str, "%d", items[itemno].value) ;
- set_cycle(W_PROPS, MAX_DEPTH, str) ;
-
- if (items[itemno].value)
- {
- items[(int) DIFF_CHOICE].value = -1 ;
- level = 1 ;
- max_depth = items[itemno].value ;
- }
- else
- {
- items[(int) DIFF_CHOICE].value = old_diffval ;
- level = old_diffval + 1 ;
- max_depth = 2 ;
- }
- draw_choice(W_PROPS, DIFF_CHOICE, W_COLOR(C_PANEL)) ;
- break ;
-
- case OPT_ANIM : opt_toggle(&ANIMATION) ;
- break ;
-
- case OPT_BEST : opt_toggle(&DO_BESTMOVE) ;
- break ;
-
- case OPT_LAST : opt_toggle(&DO_LAST) ;
- n = 63 - board.moves_left ;
- show_last(moves[n].move, DO_LAST) ;
- break ;
-
- case OPT_EVAL : opt_toggle(&SHOW_NOTES) ;
- if (SHOW_NOTES == FALSE) message(EVAL_MES, "") ;
- break ;
-
- case OPT_NUM : opt_toggle(&DO_NUMBER) ;
- n = 63 - board.moves_left ;
- if (board.moves_left < 60)
- show_number(moves[n].move, 60 - board.moves_left,
- DO_NUMBER) ;
- break ;
-
- case OPT_FLIP : opt_toggle(&QUICKGAME) ;
- break ;
-
- case OPT_MOVE : opt_toggle(&SHOW_LEGAL) ;
- break ;
-
- case OPT_CLK : opt_toggle(&DO_CLOCK) ;
- draw_clock(W_PANEL, BLACK_CLOCK, DO_CLOCK) ;
- draw_clock(W_PANEL, WHITE_CLOCK, DO_CLOCK) ;
- }
- }
-
-
- void
- show_all_moves()
- {
- if (show_moves) show_all(IS_OFF) ;
- else show_all(IS_ON) ;
- }
-
-
- void
- suggest()
- {
- int player ;
-
- if (cmode == GAME_OVER) return ;
- if (cmode == WHITE_START) player = WHITE ;
- else player = BLACK ;
- if (suggestion != -1)
- {
- sstate = (enum bltype) !((int) sstate) ;
- do_suggest(player, suggestion, snote, sstate) ;
- }
- else
- {
- set_cursor(HOURGLASS) ;
- restore_moves = show_moves ;
- if (show_moves) show_all(IS_OFF) ;
- write_to_reve(M_SUGGESTION, board.square, player, level) ;
- }
- }
-
-
- void
- undo()
- {
- enum panel_type p ;
-
- /* The amount we undo is dependant upon two things:
- *
- * 1/ Whether the opponent is the computer, in which case we undo
- * two "moves". Otherwise we just undo one "move".
- *
- * 2/ A "move" is considered to be all consecutive moves by the opponent.
- */
-
- undo_move(next_player) ;
- p = (next_player == BLACK) ? BLACK_PLAYS : WHITE_PLAYS ;
- if (items[(int) p].value == COMPUTER) undo_move(next_player) ;
-
- set_score() ;
- set_turn(next_player) ;
- }
-
-
- void
- undo_move(player)
- int player ;
- {
- int i, limit, n, x, y ;
-
- n = 63 - board.moves_left ;
- while (moves[n].player != player) n-- ;
-
- limit = (items[(int) BLACK_PLAYS].value == COMPUTER) ? 4 : 3 ;
- if (n >= limit)
- {
- if (DO_LAST) show_last(last_move, IS_OFF) ;
- if (DO_NUMBER) show_number(last_move, 60 - board.moves_left, IS_OFF) ;
- if (show_moves) show_all(IS_OFF) ;
- do_suggest(player, suggestion, note, IS_OFF) ;
- FOR_BOARD(i)
- {
- if (moves[n].square[i] != board.square[i])
- {
- get_xy(i, &x, &y) ;
- if (moves[n].square[i] == FREE)
- color_area(W_BOARD, x, y,
- 2 * pieceXrad, 2 * pieceYrad, W_COLOR(C_SQUARE)) ;
- else draw_piece(moves[n].square[i], x, y, RSRC) ;
- }
- board.square[i] = moves[n].square[i] ;
- }
- board.moves_left = moves[n].moves_left ;
- board.player = moves[n].player ;
- board.move = moves[n].move ;
- board.note = moves[n].note ;
- board.timeleft = moves[n].timeleft ;
- last_move = board.move ;
-
- FOR_BOARD(i) old_board.square[i] = moves[n-1].square[i] ;
- old_board.moves_left = moves[n-1].moves_left ;
- old_board.player = moves[n-1].player ;
- old_board.move = moves[n-1].move ;
- old_board.note = moves[n-1].note ;
- old_board.timeleft = moves[n-1].timeleft ;
-
- timeleft = board.timeleft ;
- reset_time(timeleft) ;
-
- if (OPPONENT(player) == BLACK) cmode = BLACK_START ;
- else cmode = WHITE_START ;
- message(PANEL_MES, "") ;
-
- if (n > 3)
- {
- if (DO_LAST) show_last(last_move, IS_ON) ;
- if (DO_NUMBER) show_number(last_move, 60 - board.moves_left, IS_ON) ;
- if (SHOW_NOTES) set_eval(board.player, board.move, board.note) ;
- }
- else message(EVAL_MES, "") ;
- next_player = OPPONENT(board.player) ;
- }
- else message(PANEL_MES, "No moves to undo.") ;
- }
-